package com.shop.zdy.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shop.zdy.dao.dto.McBean;
import com.shop.zdy.dao.factory.DAOFactory;
import com.shop.zdy.pub.PublicStrIsNull;

public class McManageServlet extends MyServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String fromPage = PublicStrIsNull.isNullStr(request.getParameter("fromPage"));
		String skey = PublicStrIsNull.isNullStr(request.getParameter("skey"));
		String maxType = PublicStrIsNull.isNullStr(request.getParameter("maxType"));
		String minType = PublicStrIsNull.isNullStr(request.getParameter("minType"));
		String tmpnid = PublicStrIsNull.isNullStr(request.getParameter("nid"));

		try {
			int nid = Integer.parseInt(tmpnid);
			McBean mcBean = DAOFactory.createMcDAO().selectMc(nid);
			String simg = mcBean.getSimg();
			DAOFactory.createMcDAO().deleteMc(nid);
			System.out.println(simg);
			if(!simg.equals("nopic.gif")) {
				new File(this.getServletContext().getRealPath("/mcimg/" + simg)).delete();
			}
		} catch (Exception e) {
		}
		int tymax = -1;
		int tymin = -1;
		try {
			tymax = Integer.parseInt(maxType);
		} catch (Exception e) {
		}
		try {
			tymin = Integer.parseInt(minType);
		} catch (Exception e) {
		}
		int recordSize = DAOFactory.createMcDAO().getCount(skey, -1, -1, tymax, tymin);	
		int pastart= this.pagingHandle(request, recordSize)[0];
		int paend= this.pagingHandle(request, recordSize)[1];
		request.setAttribute("mcList", DAOFactory.createMcDAO().selectMc(skey,-1, -1, tymax, tymin, pastart, paend));
		request.setAttribute("mcTypeList", DAOFactory.createMcTypeDAO()
				.searchMcType());
		request.setAttribute("skey", skey);
		request.setAttribute("typeMax", tymax);
		request.setAttribute("typeMin", tymin);

		request.getRequestDispatcher(fromPage).forward(request, response);
		}
}
